import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:series_play/comm/bindings/app_bindings.dart';
import 'package:series_play/l10n/app_localizations.dart';
import 'package:series_play/pages/splash/splash_page.dart';
import 'package:series_play/route/route_path.dart';
import 'package:series_play/utils/local_tool.dart';
import 'package:series_play/widget/app_color.dart';

import 'package:media_kit/media_kit.dart';

void main() {

  WidgetsFlutterBinding.ensureInitialized();
  MediaKit.ensureInitialized();

  PaintingBinding.instance.imageCache.maximumSizeBytes = 200 << 20; // 200MB
  PaintingBinding.instance.imageCache.maximumSize = 200; // 最多缓存 200 张图

  runApp(const MyApp());
}

GlobalKey<NavigatorState> navigatorKey = GlobalKey();

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    // 在 MaterialApp 构建时设置
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return FutureBuilder<Locale>(
          future: LocalTool.currentLocal(),
          builder: (context, local) {
            return GetMaterialApp(
              navigatorKey: navigatorKey,
              title: 'Series',
              initialBinding: AppBindings(),
              initialRoute: RoutePath.root,
              getPages: RoutePath.aliasPages,
              defaultTransition: Transition.cupertino,
              localizationsDelegates: const [
                AppLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              locale: local.data,
              supportedLocales: LocalTool.locals,
              fallbackLocale: LocalTool.locals[0],
              theme: ThemeData(
                primaryColor: AppColor.primary,
                switchTheme: SwitchThemeData(
                  // 拇指颜色
                  thumbColor: WidgetStateProperty.resolveWith<Color>((
                      Set<WidgetState> states,
                      ) {
                    if (states.contains(WidgetState.selected)) {
                      return AppColor.primary; // 选中时的颜色
                    }
                    return Colors.grey; // 未选中时的颜色
                  }),
                  // 轨道颜色
                  trackColor: WidgetStateProperty.resolveWith<Color>((
                      Set<WidgetState> states,
                      ) {
                    if (states.contains(WidgetState.selected)) {
                      return Colors.transparent; // 选中时的轨道颜色
                    }
                    return Colors.transparent; // 未选中时的轨道颜色
                  }),
                  // 轨道外围颜色
                  trackOutlineColor: WidgetStateProperty.resolveWith<Color>((
                      Set<WidgetState> states,
                      ) {
                    if (states.contains(WidgetState.selected)) {
                      return AppColor.primary; // 选中时的边框颜色
                    }
                    return Colors.grey; // 未选中时的边框颜色
                  }),
                ),
              ),
              home: SplashPage(),
            );
          },
        );
      },
    );
  }
}
